home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 4 / Example 4.13 / mesh.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-01  |  2.8 KB  |  124 lines

  1. #include "mesh.h"
  2.  
  3. MESH::MESH()
  4. {
  5.     m_pDevice = NULL;
  6.     m_pMesh = NULL;
  7. }
  8.  
  9. MESH::MESH(char fName[], IDirect3DDevice9* Dev)
  10. {
  11.     m_pDevice = Dev;
  12.     m_pMesh = NULL;
  13.     Load(fName, m_pDevice);
  14. }
  15.  
  16. MESH::~MESH()
  17. {
  18.     Release();
  19. }
  20.  
  21. HRESULT MESH::Load(char fName[], IDirect3DDevice9* Dev)
  22. {
  23.     m_pDevice = Dev;
  24.  
  25.     //Set m_white material
  26.     m_white.Ambient = m_white.Specular = m_white.Diffuse  = D3DXCOLOR(1.0f, 1.0f, 1.0f, 1.0f);
  27.     m_white.Emissive = D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f);
  28.     m_white.Power = 1.0f;
  29.  
  30.     Release();
  31.  
  32.     //Load new mesh
  33.     ID3DXBuffer * adjacencyBfr = NULL;
  34.     ID3DXBuffer * materialBfr = NULL;
  35.     DWORD noMaterials = NULL;
  36.  
  37.     if(FAILED(D3DXLoadMeshFromX(fName, D3DXMESH_MANAGED, m_pDevice,    &adjacencyBfr, &materialBfr, NULL, &noMaterials, &m_pMesh)))
  38.         return E_FAIL;
  39.  
  40.     D3DXMATERIAL *mtrls = (D3DXMATERIAL*)materialBfr->GetBufferPointer();
  41.  
  42.     for(int i=0;i<noMaterials;i++)
  43.     {
  44.         m_materials.push_back(mtrls[i].MatD3D);
  45.  
  46.         if(mtrls[i].pTextureFilename != NULL)
  47.         {
  48.             char textureFileName[90];
  49.             strcpy(textureFileName, "meshes/");
  50.             strcat(textureFileName, mtrls[i].pTextureFilename);
  51.             IDirect3DTexture9 * newTexture = NULL;
  52.             D3DXCreateTextureFromFile(m_pDevice, textureFileName, &newTexture);            
  53.             m_textures.push_back(newTexture);
  54.         }
  55.         else m_textures.push_back(NULL);
  56.     }
  57.  
  58.     m_pMesh->OptimizeInplace(D3DXMESHOPT_ATTRSORT | D3DXMESHOPT_COMPACT | D3DXMESHOPT_VERTEXCACHE,
  59.                             (DWORD*)adjacencyBfr->GetBufferPointer(), NULL, NULL, NULL);
  60.  
  61.     adjacencyBfr->Release();
  62.     materialBfr->Release();
  63.  
  64.     return S_OK;
  65. }
  66.  
  67. void MESH::Render()
  68. {
  69.     for(int i=0;i<m_materials.size();i++)
  70.     {    
  71.         if(m_textures[i] != NULL)m_pDevice->SetMaterial(&m_white);
  72.         else m_pDevice->SetMaterial(&m_materials[i]);
  73.         m_pDevice->SetTexture(0,m_textures[i]);
  74.         m_pMesh->DrawSubset(i);
  75.     }    
  76. }
  77.  
  78. void MESH::Release()
  79. {
  80.     //Clear old mesh...
  81.     if(m_pMesh != NULL)
  82.     {
  83.         m_pMesh->Release();
  84.         m_pMesh = NULL;
  85.     }
  86.  
  87.     //Clear textures and materials
  88.     for(int i=0;i<m_textures.size();i++)
  89.         if(m_textures[i] != NULL)
  90.             m_textures[i]->Release();
  91.  
  92.     m_textures.clear();
  93.     m_materials.clear();    
  94. }
  95.  
  96. MESHINSTANCE::MESHINSTANCE()
  97. {
  98.     m_pMesh = NULL;
  99.     m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  100.     m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  101. }
  102.  
  103. MESHINSTANCE::MESHINSTANCE(MESH *meshPtr)
  104. {
  105.     m_pMesh = meshPtr;
  106.     m_pos = m_rot = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  107.     m_sca = D3DXVECTOR3(1.0f, 1.0f, 1.0f);
  108. }
  109.  
  110. void MESHINSTANCE::Render()
  111. {
  112.     if(m_pMesh != NULL)
  113.     {
  114.         D3DXMATRIX p, r, s;
  115.         D3DXMatrixTranslation(&p, m_pos.x, m_pos.y, m_pos.z);
  116.         D3DXMatrixRotationYawPitchRoll(&r, m_rot.y, m_rot.x, m_rot.z);
  117.         D3DXMatrixScaling(&s, m_sca.x, m_sca.y, m_sca.z);
  118.         
  119.         D3DXMATRIX world = s * r * p;
  120.         m_pMesh->m_pDevice->SetTransform(D3DTS_WORLD, &world);
  121.  
  122.         m_pMesh->Render();
  123.     }
  124. }